#calculating diversity indices
version
install.packages("vegan")
library(vegan)

#site level data with 15 rows
hes <- read.table("Site level data.csv", header=TRUE, row.names='Site', sep=",")
names(hes)

invsimp <- diversity(hes, "inv")
write.csv(invsimp, "sitesinvsimp")
shannon <- diversity(hes, "shannon")
write.csv(shannon, "sitesshannon")

#trap level data with 30 rows
hes2 <- read.table("Trap level data.csv", header=TRUE, row.names='Site', sep=",")
names(hes2)


invsimp <- diversity(hes2, "inv")
write.csv(invsimp, "trapsinvsimp")
shannon <- diversity(hes2, "shannon")
write.csv(shannon, "trapsshannon")

#species accumulation curve via trap and individuals
library(vegan)
hes2 <- read.table("Trap level data.csv", header=TRUE, row.names='Site', sep=",")
names(hes2)
x<-specaccum(hes2, method = "rarefaction")
#first in base code
plot(x, xvar= "individuals", ci=2, xlim=c(0, 1600),xaxp=c(0, 1600, 8), ci.col= "gray", ci.type="polygon", ci.lty = 2, xlab = "individuals", 
     ylab = "species") 
#now in ggplot2
library(ggplot2)
#first create table of data with sites, individuals, richness and sd from x
fig3btab<-data.frame(x$individuals, x$richness)
minussd<-(x$richness-x$sd)
plussd<-(x$richness+x$sd)
fig3btab$minussd<-minussd
fig3btab$plussd<-plussd
str(fig3btab)
#set theme
cleanup=theme(panel.grid.major = element_blank(),
              panel.grid.minor = element_blank(),
              panel.background = element_blank(),
              axis.line = element_line(color = "black"),
              axis.line.x.top = element_line(color = "black"),
              axis.line.y.right = element_line(color = "black"))

Figure3b=ggplot(fig3btab, aes(fig3btab$x.individuals))
Figure3b+
  xlab("Individuals")+
  ylab("Species")+
  geom_line(aes(y=fig3btab$x.richness), color="black")+
  geom_line(aes(y=fig3btab$plussd), linetype="dashed", color="black")+
  geom_line(aes(y=fig3btab$minussd), linetype="dashed", color="black")+
  scale_y_continuous(breaks = seq(0, 100, by = 20), limits=c(0,100))+
  scale_x_continuous(breaks = seq(0, 1500, by = 500))+
  cleanup          


#plot of total plus zone species accumulation
hes2 <- read.table("Trap level data.csv", header=TRUE, row.names='Site', sep=",")
names(hes2)
x<-specaccum(hes2, method = "rarefaction")
plot(x, xvar= "individuals", log="y", ci=0, xlim=c(0, 1600),xaxp=c(0, 1600, 8),  xlab = "individuals", 
     ylab = "species") 

#add literature data
lit <- read.table("literature data.csv", header=TRUE, sep=",")
names(lit)
points(lit$Individuals, lit$Species)

#add line for lowest zone
zone1<-read.table("110-250.csv", row.names='Site', header=TRUE, sep=",")
z1<-specaccum(zone1, method = "rarefaction")
lines(z1, xvar= "individuals", ci=0)
#add line for next zone up
zone2<-read.table("332-549.csv", row.names='Site', header=TRUE, sep=",")
z2<-specaccum(zone2, method = "rarefaction")
lines(z2, xvar= "individuals", ci=0)
#add line for next zone up
zone3<-read.table("703-877.csv", row.names='Site', header=TRUE, sep=",")
z3<-specaccum(zone3, method = "rarefaction")
lines(z3, xvar= "individuals", ci=0)
#add line for next zone up
zone4<-read.table("952-1071.csv", row.names='Site', header=TRUE, sep=",")
z4<-specaccum(zone4, method = "rarefaction")
lines(z4, xvar= "individuals", ci=0)
#add line for next zone up
zone5<-read.table("1236-1482.csv", row.names='Site', header=TRUE, sep=",")
z5<-specaccum(zone5, method = "rarefaction")
lines(z5, xvar= "individuals", ci=0)
#add line for next zone up
zone6<-read.table("1649-1812.csv", row.names='Site', header=TRUE, sep=",")
z6<-specaccum(zone6, method = "rarefaction")
lines(z6, xvar= "individuals", ci=0)
#add line for next zone up
zone7<-read.table("1935-2169.csv", row.names='Site', header=TRUE, sep=",")
z7<-specaccum(zone7, method = "rarefaction")
lines(z7, xvar= "individuals", ci=0)

#plot graph in ggplot2
#set up datatable first
fig3dtab<-data.frame(x$individuals, x$richness)
fig3dtab$z1rich<-c(17.81, 24.59, 28.73, 32, rep(NA, 26))
fig3dtab$z1inds<-c(41, 82, 122, 163, rep(NA, 26))
fig3dtab$z2rich<-c(z2$richness, rep(NA,26))
fig3dtab$z2inds<-c(z2$individuals, rep(NA,26))
fig3dtab$z3rich<-c(z3$richness, rep(NA,26))
fig3dtab$z3inds<-c(z3$individuals, rep(NA,26))
fig3dtab$z4rich<-c(z4$richness, rep(NA,26))
fig3dtab$z4inds<-c(z4$individuals, rep(NA,26))
fig3dtab$z5rich<-c(z5$richness, rep(NA,26))
fig3dtab$z5inds<-c(z5$individuals, rep(NA,26))
fig3dtab$z6rich<-c(z6$richness, rep(NA,26))
fig3dtab$z6inds<-c(z6$individuals, rep(NA,26))
fig3dtab$z7rich<-c(z7$richness, rep(NA,24))
fig3dtab$z7inds<-c(z7$individuals, rep(NA,24))

#now plot combined line graph, using multiple x-axes and dataframes
library(ggplot2)
#first create table of data with sites, individuals, richness and sd from x

#set theme
cleanup=theme(panel.grid.major = element_blank(),
              panel.grid.minor = element_blank(),
              panel.background = element_blank(),
              axis.line = element_line(color = "black"),
              axis.line.x.top = element_line(color = "black"),
              axis.line.y.right = element_line(color = "black"))

Figur3d=ggplot()+
  xlab("Individuals")+
  ylab("Species")+
  geom_line(data=fig3dtab, aes(y=x.richness, x.individuals), color="black")+
  geom_line(data=fig3dtab, aes(y=fig3dtab$z1rich, z1inds), color="black")+
  geom_line(data=fig3dtab, aes(y=fig3dtab$z2rich, z2inds), color="black")+
  geom_line(data=fig3dtab, aes(y=fig3dtab$z3rich, z3inds), color="black")+
  geom_line(data=fig3dtab, aes(y=fig3dtab$z4rich, z4inds), color="black")+
  geom_line(data=fig3dtab, aes(y=fig3dtab$z5rich, z5inds), color="black")+
  geom_line(data=fig3dtab, aes(y=fig3dtab$z6rich, z6inds), color="black")+
  geom_line(data=fig3dtab, aes(y=fig3dtab$z7rich, z7inds), color="black")+
  geom_point(data=lit, aes(y=Species, x=Individuals), shape=1, color="black")+
  scale_y_continuous(trans="log10", limits=c(1,100))+
  scale_x_continuous(breaks = seq(0, 1500, by = 500), limits=c(0,1500))+
  cleanup    

#plot how estimators increase with sampling
S1<-poolaccum(hes2)
summary(S1)
plot(S1)
write.csv(S1$means, "trappoolplot")

hes4<-read.table("trappoolplot.csv", header=TRUE, sep=",")
names(hes4)
jpeg("trappoolplot.jpeg", quality=100)
plot(1, type="n", xlab="traps", ylab="species", xlim=c(0, 30), ylim=c(0, 150))
lines(hes4$N, hes4$S, lty=1)
points(hes4$N, hes4$S, pch=16)
lines(hes4$N, hes4$Chao, lty=1)
points(hes4$N, hes4$Chao, pch=1)
lines(hes4$N, hes4$Jackknife.1, lty=1)
points(hes4$N, hes4$Jackknife.1, pch=0)
lines(hes4$N, hes4$Jackknife.2, lty=1)
points(hes4$N, hes4$Jackknife.2, pch=2)
lines(hes4$N, hes4$Bootstrap, lty=1)
points(hes4$N, hes4$Bootstrap, pch=5)
dev.off()

#now in ggplot2
Figure3a=ggplot()+
  xlab("Traps")+
  ylab("Species")+
  geom_line(data=hes4, aes(y=Chao, N), color="black")+
  geom_point(data=hes4, aes(y=Chao, x=N), shape=2)+
  geom_line(data=hes4, aes(y=Jackknife.1, N), color="black")+
  geom_point(data=hes4, aes(y=Jackknife.1, x=N), shape=5)+
  geom_line(data=hes4, aes(y=Jackknife.2, N), color="black")+
  geom_point(data=hes4, aes(y=Jackknife.2, x=N), shape=0)+
  geom_line(data=hes4, aes(y=Bootstrap, N), color="black")+
  geom_point(data=hes4, aes(y=Bootstrap, x=N), shape=1)+
  geom_line(data=hes4, aes(y=S, N), color="black")+
  geom_point(data=hes4, aes(y=S, x=N), shape=19, color="black")+
  scale_y_continuous(breaks = seq(0, 150, by = 50), limits=c(1,150))+
  scale_x_continuous(breaks = seq(0, 30, by = 2), limits=c(0,30))+
  cleanup   

y<-specaccum(hes2, method = "random",  permutations=1000)
plot(y, xvar= "sites", ci.col= "gray", ci.type="polygon", ci.lty = 2, xlab = "traps", 
     ylab = "species")

#Now plot this in ggplot
#now in ggplot2
library(ggplot2)
#first create table of data with sites, individuals, richness and sd from x
fig3ctab<-data.frame(y$sites, y$richness)
minussd<-(y$richness-y$sd)
plussd<-(y$richness+y$sd)
fig3ctab$minussd<-minussd
fig3ctab$plussd<-plussd
str(fig3ctab)
#set theme
cleanup=theme(panel.grid.major = element_blank(),
              panel.grid.minor = element_blank(),
              panel.background = element_blank(),
              axis.line = element_line(color = "black"),
              axis.line.x.top = element_line(color = "black"),
              axis.line.y.right = element_line(color = "black"))

Figure3c=ggplot(fig3ctab, aes(fig3ctab$y.sites))+

  xlab("Traps")+
  ylab("Species")+
  geom_line(aes(y=fig3ctab$y.richness), color="black")+
  geom_line(aes(y=fig3ctab$plussd), linetype="dashed", color="black")+
  geom_line(aes(y=fig3ctab$minussd), linetype="dashed", color="black")+
  scale_y_continuous(breaks = seq(0, 100, by = 20), limits=c(0,100))+
  scale_x_continuous(breaks = seq(0, 30, by = 2))+
  cleanup          


hes <- read.table("Site level data.csv", header=TRUE, row.names='Site', sep=",")
names(hes)

w<-specaccum(hes, method = "rarefaction")
plot(w, xvar= "individuals", ci=2, xlim=c(0, 1600), xaxp=c(0, 1600, 8), ci.col= "gray", ci.type="polygon", ci.lty = 2, xlab = "individuals", 
     ylab = "species") 

z<-specaccum(hes, method = "random",  permutations=1000)
plot(z, xvar= "sites", ci.col= "gray", xaxp=c(0, 15, 15), ci.type="polygon", ci.lty = 2, xlab = "sites", 
     ylab = "species")

#Now plot this in ggplot
#now in ggplot2
library(ggplot2)
#first create table of data with sites, individuals, richness and sd from x
fig3btab<-data.frame(z$sites, z$richness)
minussd<-(z$richness-z$sd)
plussd<-(z$richness+z$sd)
fig3btab$minussd<-minussd
fig3btab$plussd<-plussd
str(fig3btab)
#set theme
cleanup=theme(panel.grid.major = element_blank(),
              panel.grid.minor = element_blank(),
              panel.background = element_blank(),
              axis.line = element_line(color = "black"),
              axis.line.x.top = element_line(color = "black"),
              axis.line.y.right = element_line(color = "black"))

Figure3b=ggplot(fig3btab, aes(fig3btab$z.sites))+
  xlab("Sites")+
  ylab("Species")+
  geom_line(aes(y=fig3btab$z.richness), color="black")+
  geom_line(aes(y=fig3btab$plussd), linetype="dashed", color="black")+
  geom_line(aes(y=fig3btab$minussd), linetype="dashed", color="black")+
  scale_y_continuous(breaks = seq(0, 100, by = 20), limits=c(0,100))+
  scale_x_continuous(breaks = seq(0, 15, by = 1))+
  cleanup          


specpool(hes2)
specpool(hes)

hes3 <- read.table("Poolabundance.csv", header=TRUE, sep=",")
names(hes3)
estimateR(hes3)

#now combine the four plots into Figure1. 
install.packages("ggpubr")
library("ggpubr")
Figure3<-ggarrange(Figure3a, Figure3b, Figure3c, Figur3d, 
                   labels = c("(a)", "(b)", "(c)", "(d)"),
                   hjust=0,
                   ncol = 2, nrow = 2)

ggsave("Figure3.tiff", units="cm", width=20, height=15, dpi=300, compression = 'lzw')



#analyses of altitude at trap level.
install.packages("AICcmodavg")
library(AICcmodavg)
alt1<-read.table("trap level summary data.csv", header=TRUE, sep=",")
names(alt1)
e2<-alt1$elevation^2
e3<-alt1$elevation^3
e4<-alt1$elevation^4
logabun<-log10(alt1$Total.abundance)
mod1<-lm(alt1$Total.abundance~alt1$elevation)
summary(mod1)
plot(alt1$elevation, alt1$Total.abundance)
hist(alt1$Total.abundance)
par(mfrow=c(2,2))
plot(mod1)
summary(mod1)
AICc(mod1, second.ord=TRUE)
#residuals look OK, stick with simple lm and untransformed data

par(mfrow=c(1,1))
mod2<-lm(alt1$Total.abundance~alt1$elevation+e2)
summary(mod2)
plot(alt1$elevation, alt1$Total.abundance)

e2.dat<-data.frame(x=c(110:2170))

e2.dat$y<-1.960e+01+(8.820e-02*e2.dat$x)+(-3.946e-05*(e2.dat$x^2))
lines(e2.dat$x, e2.dat$y)
lines(alt1$elevation, predict(mod2))
par(mfrow=c(2,2))
plot(mod2)
AICc(mod2, second.ord=TRUE)

mod3<-lm(alt1$Total.abundance~alt1$elevation+e2+e3)
summary(mod3)
plot(alt1$elevation, alt1$Total.abundance)

e2.dat<-data.frame(x=c(110:2170))

e2.dat$y2<-6.210e+01+(-1.217e-01*e2.dat$x)+(1.912e-04*(e2.dat$x^2)
                                            +(-6.835e-08*e2.dat$x^3))
lines(e2.dat$x, e2.dat$y2)
lines(alt1$elevation, predict(mod3))
par(mfrow=c(2,2))
plot(mod3)
par(mfrow=c(1,1))
AICc(mod3, second.ord=TRUE)

mod4<-lm(alt1$Total.abundance~alt1$elevation+e2+e3+e4)
summary(mod4)
plot(alt1$elevation, alt1$Total.abundance)
lines(alt1$elevation, predict(mod4))

par(mfrow=c(2,2))
plot(mod4)
par(mfrow=c(1,1))
AICc(mod4, second.ord=TRUE)



#now for species richness against altitude
logrich<-log10(alt1$Total.trap.richness)
names(alt1)
mod1<-lm(logrich~alt1$elevation)
summary(mod1)
par(mfrow=c(1,1))
plot(alt1$elevation, logrich)
lines(alt1$elevation, predict(mod1))
hist(logrich)
par(mfrow=c(2,2))
plot(mod1)
summary(mod1)
AICc(mod1, second.ord=TRUE)
#residuals look OK, stick with log10 trasnformed richness

mod2<-lm(logrich~alt1$elevation+e2)
summary(mod2)
par(mfrow=c(1,1))
plot(alt1$elevation, logrich)
lines(alt1$elevation, predict(mod2))
par(mfrow=c(2,2))
plot(mod2)
AICc(mod2, second.ord=TRUE)

mod3<-lm(logrich~alt1$elevation+e2+e3)
summary(mod3)
par(mfrow=c(1,1))
plot(alt1$elevation, logrich)


lines(alt1$elevation, predict(mod3))
par(mfrow=c(2,2))
plot(mod3)
par(mfrow=c(1,1))

AICc(mod3, second.ord=TRUE)

mod4<-lm(logrich~alt1$elevation+e2+e3+e4)
summary(mod4)
par(mfrow=c(1,1))
plot(alt1$elevation, logrich)
lines(alt1$elevation, predict(mod4))

par(mfrow=c(2,2))
plot(mod4)
par(mfrow=c(1,1))
AICc(mod4, second.ord=TRUE)



#onto Invsim vesus altitude
mod1<-lm(alt1$Invsim~alt1$elevation)
summary(mod1)
par(mfrow=c(1,1))
plot(alt1$elevation, alt1$Invsim)
lines(alt1$elevation, predict(mod1))
hist(alt1$Invsim)
par(mfrow=c(2,2))
plot(mod1)
AICc(mod1, second.ord=TRUE)
#residuals look a bit dodgy here: see how the look with higher order models

mod2<-lm(alt1$Invsim~alt1$elevation+e2)
summary(mod2)
par(mfrow=c(1,1))
plot(alt1$elevation, alt1$Invsim)
lines(alt1$elevation, predict(mod2))
par(mfrow=c(2,2))
plot(mod2)
AICc(mod2, second.ord=TRUE)

mod3<-lm(alt1$Invsim~alt1$elevation+e2+e3)
summary(mod3)
par(mfrow=c(1,1))
plot(alt1$elevation, alt1$Invsim)


lines(alt1$elevation, predict(mod3))
par(mfrow=c(2,2))
plot(mod3)
par(mfrow=c(1,1))
AICc(mod3, second.ord=TRUE)

mod4<-lm(alt1$Invsim~alt1$elevation+e2+e3+e4)
summary(mod4)
par(mfrow=c(1,1))
plot(alt1$elevation, alt1$Invsim)
lines(alt1$elevation, predict(mod4))
par(mfrow=c(2,2))
plot(mod4)
par(mfrow=c(1,1))
AICc(mod4, second.ord=TRUE)

#onto Shannon vesus altitude
sqrtshan<-sqrt(alt1$shannon)
mod1<-lm(alt1$shannon~alt1$elevation)
summary(mod1)
par(mfrow=c(1,1))
plot(alt1$elevation, alt1$shannon)
lines(alt1$elevation, predict(mod1))
hist(alt1$shannon)
par(mfrow=c(2,2))
plot(mod1)
AICc(mod1, second.ord=TRUE)
#try with sqrt and untrasnformed; looks best untransformed

mod2<-lm(alt1$shannon~alt1$elevation+e2)
summary(mod2)
par(mfrow=c(1,1))
plot(alt1$elevation, alt1$shannon)
lines(alt1$elevation, predict(mod2))
par(mfrow=c(2,2))
plot(mod2)
AICc(mod2, second.ord=TRUE)

mod3<-lm(alt1$shannon~alt1$elevation+e2+e3)
summary(mod3)
par(mfrow=c(1,1))
plot(alt1$elevation, alt1$shannon)
lines(alt1$elevation, predict(mod3))
par(mfrow=c(2,2))
plot(mod3)
par(mfrow=c(1,1))
AICc(mod3, second.ord=TRUE)

mod4<-lm(alt1$shannon~alt1$elevation+e2+e3+e4)
summary(mod4)
par(mfrow=c(1,1))
plot(alt1$elevation, alt1$shannon)
lines(alt1$elevation, predict(mod4))
par(mfrow=c(2,2))
plot(mod4)
par(mfrow=c(1,1))
AICc(mod4, second.ord=TRUE)

#looks best with untransformed data

#try Invsim logged
logins<-log10(alt1$Invsim)
mod1<-lm(logins~alt1$elevation)
summary(mod1)
par(mfrow=c(1,1))
plot(alt1$elevation, logins)
lines(alt1$elevation, predict(mod1))
hist(logins)
par(mfrow=c(2,2))
plot(mod1)
AICc(mod1, second.ord=TRUE)
#residuals look better

mod2<-lm(logins~alt1$elevation+e2)
summary(mod2)
par(mfrow=c(1,1))
plot(alt1$elevation, logins)
lines(alt1$elevation, predict(mod2))
par(mfrow=c(2,2))
plot(mod2)
AICc(mod2, second.ord=TRUE)

mod3<-lm(logins~alt1$elevation+e2+e3)
summary(mod3)
par(mfrow=c(1,1))
plot(alt1$elevation, logins)
lines(alt1$elevation, predict(mod3))
par(mfrow=c(2,2))
plot(mod3)
par(mfrow=c(1,1))
AICc(mod3, second.ord=TRUE)

mod4<-lm(logins~alt1$elevation+e2+e3+e4)
summary(mod4)
par(mfrow=c(1,1))
plot(alt1$elevation, logins)
lines(alt1$elevation, predict(mod4))
par(mfrow=c(2,2))
plot(mod4)
par(mfrow=c(1,1))
AICc(mod4, second.ord=TRUE)

#Plot Figure 4
library(ggplot2)

#set theme
cleanup=theme(panel.grid.major = element_blank(),
              panel.grid.minor = element_blank(),
              panel.background = element_blank(),
              axis.line = element_line(color = "black"),
              axis.line.x.top = element_line(color = "black"),
              axis.line.y.right = element_line(color = "black"))

#Plot Fig4a


 
  Fig4a = ggplot(alt1, aes(elevation, Total.abundance))+
   geom_point()+
    xlab("Elevation, m")+
    ylab("Abundance")+
    geom_smooth(method=lm, formula = y~poly(x, 3), color="black", fullrange=FALSE)+
    scale_y_continuous(breaks = seq(0, 140, by = 20))+
    scale_x_continuous(breaks = seq(0, 2000, by = 500))+
    cleanup                       
 
#Plot Fig4b
Fig4b=ggplot(alt1, aes(elevation, log10(Total.trap.richness)))+
geom_point()+
  xlab("Elevation, m")+
  ylab(bquote(Log[10]~'Richness'))+
  geom_smooth(method=lm, formula = y~poly(x, 3), color="black", fullrange=FALSE)+
  scale_y_continuous(breaks = seq(0, 1.5, by = 0.2))+
  scale_x_continuous(breaks = seq(0, 2000, by = 500))+
  cleanup

#Fig4c
Fig4c=ggplot(alt1, aes(elevation, log10(Invsim)))+
geom_point()+
  xlab("Elevation, m")+
  ylab(bquote(Log[10] (1/D)))+
  geom_smooth(method=lm, formula = y~poly(x, 2), color="black", fullrange=FALSE)+
  scale_y_continuous(breaks = seq(0, 1.6, by = 0.2))+
  scale_x_continuous(breaks = seq(0, 2000, by = 500))+
  cleanup
#Fig4d
Fig4d<-ggplot(alt1, aes(elevation, shannon))+
geom_point()+
  xlab("Elevation, m")+
  ylab("Shannon Index")+
  geom_smooth(method=lm, formula = y~poly(x, 2), color="black", fullrange=FALSE)+
  scale_y_continuous(breaks = seq(0, 3.5, by = 0.5))+
  scale_x_continuous(breaks = seq(0, 2000, by = 500))+
  cleanup



install.packages("ggpubr")
library("ggpubr")
Figure4<-ggarrange(Fig4a, Fig4b, Fig4c, Fig4d, 
          labels = c("(a)", "(b)", "(c)", "(d)"),
          hjust = 0,
          ncol = 2, nrow = 2)

ggsave("Figure4.tiff", units="cm", width=20, height=15, dpi=300, compression = 'lzw')

#now analyses of altitude at site level
install.packages("AICcmodavg")
library(AICcmodavg)
alt1<-read.table("site level summary.csv", header=TRUE, sep=",")
names(alt1)
e2<-alt1$elevation^2
e3<-alt1$elevation^3
e4<-alt1$elevation^4
mod1<-lm(alt1$Total.site.abundance~alt1$elevation)
summary(mod1)
plot(alt1$elevation, alt1$Total.site.abundance)
hist(alt1$Total.site.abundance)
par(mfrow=c(2,2))
plot(mod1)
summary(mod1)
AICc(mod1, second.ord=TRUE)
#residuals look OK, stick with simple lm and untransformed data

mod2<-lm(alt1$Total.site.abundance~alt1$elevation+e2)
summary(mod2)
par(mfrow=c(1,1))
plot(alt1$elevation, alt1$Total.site.abundance)

e2.dat<-data.frame(x=c(110:2170))

e2.dat$y<-3.9560e+01+(1.752e-01*e2.dat$x)+(-7.834e-05*(e2.dat$x^2))
lines(e2.dat$x, e2.dat$y)
par(mfrow=c(2,2))
plot(mod2)
AICc(mod2, second.ord=TRUE)

mod3<-lm(alt1$Total.site.abundance~alt1$elevation+e2+e3)
summary(mod3)
par(mfrow=c(1,1))

plot(alt1$elevation, alt1$Total.site.abundance)

e2.dat<-data.frame(x=c(110:2170))

e2.dat$y2<-1.249e+02+(-2.458e-01*e2.dat$x)+(3.844e-04*(e2.dat$x^2)
                                            +(-1.371e-07*e2.dat$x^3))
lines(e2.dat$x, e2.dat$y2)
par(mfrow=c(2,2))
plot(mod2)
par(mfrow=c(1,1))
AICc(mod3, second.ord=TRUE)

mod4<-lm(alt1$Total.site.abundance~alt1$elevation+e2+e3+e4)
summary(mod4)
plot(alt1$elevation, alt1$Total.site.abundance)
lines(alt1$elevation, predict(mod4))

par(mfrow=c(2,2))
plot(mod2)
par(mfrow=c(1,1))
AICc(mod4, second.ord=TRUE)

#now species richness versus altitude at site level
#use log10 transformation based on trap level data
names(alt1)
logrich<-log10(alt1$Total.site.richness)
mod1<-lm(logrich~alt1$elevation)
summary(mod1)
par(mfrow=c(1,1))
plot(alt1$elevation, logrich)
lines(alt1$elevation, predict(mod1))
hist(logrich)
par(mfrow=c(2,2))
plot(mod1)
summary(mod1)
AICc(mod1, second.ord=TRUE)
#residuals look OK, stick with simple lm and untransformed data

mod2<-lm(logrich~alt1$elevation+e2)
summary(mod2)
par(mfrow=c(1,1))
plot(alt1$elevation, logrich)
lines(alt1$elevation, predict(mod2))
par(mfrow=c(2,2))
plot(mod2)
AICc(mod2, second.ord=TRUE)

mod3<-lm(logrich~alt1$elevation+e2+e3)
summary(mod3)
par(mfrow=c(1,1))
plot(alt1$elevation, logrich)


lines(alt1$elevation, predict(mod3))
par(mfrow=c(2,2))
plot(mod3)
par(mfrow=c(1,1))
AICc(mod3, second.ord=TRUE)

mod4<-lm(logrich~alt1$elevation+e2+e3+e4)
summary(mod4)
par(mfrow=c(1,1))
plot(alt1$elevation, logrich)
lines(alt1$elevation, predict(mod4))

par(mfrow=c(2,2))
plot(mod4)
par(mfrow=c(1,1))
AICc(mod4, second.ord=TRUE)

#now invsimp against altitude for sites
mod1<-lm(alt1$invsim~alt1$elevation)
summary(mod1)
par(mfrow=c(1,1))
plot(alt1$elevation, alt1$invsim)
lines(alt1$elevation, predict(mod1))
hist(alt1$invsim)
par(mfrow=c(2,2))
plot(mod1)
AICc(mod1, second.ord=TRUE)
#residuals look a bit dodgy here: see how the look with higher order models

mod2<-lm(alt1$invsim~alt1$elevation+e2)
summary(mod2)
par(mfrow=c(1,1))
plot(alt1$elevation, alt1$invsim)
lines(alt1$elevation, predict(mod2))
par(mfrow=c(2,2))
plot(mod2)
AICc(mod2, second.ord=TRUE)

mod3<-lm(alt1$invsim~alt1$elevation+e2+e3)
summary(mod3)
par(mfrow=c(1,1))
plot(alt1$elevation, alt1$invsim)

lines(alt1$elevation, predict(mod3))
par(mfrow=c(2,2))
plot(mod3)
par(mfrow=c(1,1))
AICc(mod3, second.ord=TRUE)

mod4<-lm(alt1$invsim~alt1$elevation+e2+e3+e4)
summary(mod4)
par(mfrow=c(1,1))
plot(alt1$elevation, alt1$invsim)
lines(alt1$elevation, predict(mod4))
par(mfrow=c(2,2))
plot(mod4)
par(mfrow=c(1,1))
AICc(mod4, second.ord=TRUE)

#onto Shannon vesus altitude for sites
mod1<-lm(alt1$shannon~alt1$elevation)
summary(mod1)
par(mfrow=c(1,1))
plot(alt1$elevation, alt1$shannon)
lines(alt1$elevation, predict(mod1))
hist(alt1$shannon)
par(mfrow=c(2,2))
plot(mod1)
AICc(mod1, second.ord=TRUE)
#residuals look a bit dodgy here: see how the look with higher order models

mod2<-lm(alt1$shannon~alt1$elevation+e2)
summary(mod2)
par(mfrow=c(1,1))
plot(alt1$elevation, alt1$shannon)
lines(alt1$elevation, predict(mod2))
par(mfrow=c(2,2))
plot(mod2)
AICc(mod2, second.ord=TRUE)

mod3<-lm(alt1$shannon~alt1$elevation+e2+e3)
summary(mod3)
par(mfrow=c(1,1))
plot(alt1$elevation, alt1$shannon)
lines(alt1$elevation, predict(mod3))
par(mfrow=c(2,2))
plot(mod3)
par(mfrow=c(1,1))
AICc(mod3, second.ord=TRUE)

mod4<-lm(alt1$shannon~alt1$elevation+e2+e3+e4)
summary(mod4)
par(mfrow=c(1,1))
plot(alt1$elevation, alt1$shannon)
lines(alt1$elevation, predict(mod4))
par(mfrow=c(2,2))
plot(mod4)
par(mfrow=c(1,1))
AICc(mod4, second.ord=TRUE)

#try Invsim logged for sites against altitude
logins<-log10(alt1$invsim)
mod1<-lm(logins~alt1$elevation)
summary(mod1)
par(mfrow=c(1,1))
plot(alt1$elevation, logins)
lines(alt1$elevation, predict(mod1))
hist(logins)
par(mfrow=c(2,2))
plot(mod1)
AICc(mod1, second.ord=TRUE)
#residuals look better

mod2<-lm(logins~alt1$elevation+e2)
summary(mod2)
par(mfrow=c(1,1))
plot(alt1$elevation, logins)
lines(alt1$elevation, predict(mod2))
par(mfrow=c(2,2))
plot(mod2)
AICc(mod2, second.ord=TRUE)

mod3<-lm(logins~alt1$elevation+e2+e3)
summary(mod3)
par(mfrow=c(1,1))
plot(alt1$elevation, logins)
lines(alt1$elevation, predict(mod3))
par(mfrow=c(2,2))
plot(mod3)
par(mfrow=c(1,1))
AICc(mod3, second.ord=TRUE)

mod4<-lm(logins~alt1$elevation+e2+e3+e4)
summary(mod4)
par(mfrow=c(1,1))
plot(alt1$elevation, logins)
lines(alt1$elevation, predict(mod4))
par(mfrow=c(2,2))
plot(mod4)
par(mfrow=c(1,1))
AICc(mod4, second.ord=TRUE)

#now cubic models of temperature against abundance for sites
T2<-alt1$meanT^2
T3<-alt1$meanT^3
T4<-alt1$meanT^4
mod3<-lm(alt1$Total.site.abundance~alt1$meanT+T2+T3)
summary(mod3)
par(mfrow=c(1,1))
plot(alt1$meanT, alt1$Total.site.abundance)
lines(alt1$meanT, predict(mod3))
par(mfrow=c(2,2))
plot(mod3)
AICc(mod3, second.ord=TRUE)

#now quadratic model of temperature against log richness for sites
mod2<-lm(logrich~alt1$meanT+T2)
summary(mod2)
par(mfrow=c(1,1))
plot(alt1$meanT, logrich)
lines(alt1$meanT, predict(mod2))
par(mfrow=c(2,2))
plot(mod2)
AICc(mod2, second.ord=TRUE)

#now linear model of temperature against loginvs for sites
mod1<-lm(logins~alt1$meanT)
summary(mod1)
par(mfrow=c(1,1))
plot(alt1$meanT, logins)
lines(alt1$meanT, predict(mod1))
par(mfrow=c(2,2))
plot(mod1)
AICc(mod1, second.ord=TRUE)

#now quadratic model of tmeperature against shannon for sites
mod22<-lm(alt1$shannon~alt1$meanT+T2)
summary(mod22)
par(mfrow=c(1,1))
plot(alt1$meanT, alt1$shannon)
lines(alt1$meanT, predict(mod22))
par(mfrow=c(2,2))
plot(mod22)
AICc(mod22, second.ord=TRUE)

#now produce Figure 5a of abundance and temperature across sites
#set theme
library(ggplot2)
cleanup=theme(panel.grid.major = element_blank(),
              panel.grid.minor = element_blank(),
              panel.background = element_blank(),
              axis.line = element_line(color = "black"),
              axis.line.x.top = element_line(color = "black"),
              axis.line.y.right = element_line(color = "black"))

#Plot Fig5a



Fig5a = ggplot(alt1, aes(meanT, Total.site.abundance))+
  geom_point()+
  xlab(bquote('Temperature, '^'o'*C))+
  ylab("Abundance")+
  coord_cartesian(ylim=c(0,240)) +
  geom_smooth(method=lm, formula = y~poly(x, 3), color="black", fullrange=FALSE)+
  scale_y_continuous(breaks = seq(0, 240, by = 20))+
  scale_x_continuous(breaks = seq(12, 23, by = 1), limits=c(12,23))+
  cleanup                       

#Plot Fig5b
Fig5b=ggplot(alt1, aes(meanT, log10(Total.site.richness)))+
  geom_point()+
  xlab(bquote('Temperature, '^'o'*C))+
  ylab(bquote(Log[10]~'Richness'))+
  coord_cartesian(ylim=c(0,1.8)) +
  geom_smooth(method=lm, formula = y~poly(x, 2), color="black", fullrange=FALSE)+
  scale_y_continuous(breaks = seq(0, 1.8, by = 0.2))+
  scale_x_continuous(breaks = seq(12, 23, by = 1), limits=c(12,23))+
  cleanup

#Fig5c
Fig5c=ggplot(alt1, aes(meanT, log10(invsim)))+
  geom_point()+
  xlab(bquote('Temperature, '^'o'*C))+
  ylab(bquote(Log[10] (1/D)))+
  coord_cartesian(ylim=c(0,1.6)) +
  geom_smooth(method=lm, color="black", fullrange=FALSE)+
  scale_y_continuous(breaks = seq(0, 1.6, by = 0.2))+
  scale_x_continuous(breaks = seq(12, 23, by = 1), limits=c(12,23))+
  cleanup
#Fig5d
Fig5d<-ggplot(alt1, aes(meanT, shannon))+
  geom_point()+
  xlab(bquote('Temperature, '^'o'*C))+
  ylab("Shannon Index")+
  coord_cartesian(ylim=c(0,3.5)) +
  geom_smooth(method=lm, formula = y~poly(x, 2), color="black", fullrange=FALSE)+
  scale_y_continuous(breaks = seq(0, 3.5, by = 0.5))+
  scale_x_continuous(breaks = seq(12, 23, by = 1), limits=c(12,23))+
  cleanup



install.packages("ggpubr")
library("ggpubr")
Figure5<-ggarrange(Fig5a, Fig5b, Fig5c, Fig5d, 
                   labels = c("(a)", "(b)", "(c)", "(d)"),
                   hjust=0,
                   ncol = 2, nrow = 2)

ggsave("Figure5.tiff", units="cm", width=20, height=15, dpi=300, compression = 'lzw')


#now community dissimilarity (Bray-Curtis) across sites,, and Mantel tests of distance
install.packages("vegan")
library(vegan)
hes <- read.table("Site level data.csv", header=TRUE, row.names='Site', sep=",")
names(hes)
bray1<-vegdist(hes, method="bray")
write.csv(as.matrix(bray1), "braysitescores")
dist.elev = dist(alt1$elevation, method = "euclidean")
mant1  = mantel(dist.elev, bray1, method = "spearman", permutations = 9999, na.rm = TRUE)

#now community dissimilarity and Mantel tests for traps. 
hes <- read.table("Trap level data.csv", header=TRUE, row.names='Site', sep=",")
names(hes)
bray2<-vegdist(hes, method="bray")
write.csv(as.matrix(bray2), "braytrapscores")
dist.elev = dist(alt1$elevation, method = "euclidean")
mant2  = mantel(dist.elev, bray1, method = "spearman", permutations = 9999, na.rm = TRUE)

#Spearman correlations between all variables at site level
hes3 <- read.table("site level summary.csv", header=TRUE, row.names='Site', sep=",")
names(hes3)
#get spearman correlation coefficients
cor.sites<-cor(hes3[,c(1:10,12,14,15,18,21:27)], method='spearman')
write.csv(cor.sites, "site correlation matrix")
#Now get p values
install.packages("Hmisc")
library(Hmisc)
corsites.ps<-rcorr(as.matrix(hes3[,c(1:10,12,14,15,18,21:27)]), type="spearman")$P
write.csv(corsites.ps, "site correlation matrix")

#now the same at trap level
hes4 <- read.table("trap level summary data.csv", header=TRUE, row.names="Trap", sep=",")
names(hes4)
#get spearman correlation coefficients
cor.traps<-cor(hes4[,c(1:5,7,9,10,13,16:22)], method='spearman')
write.csv(cor.traps, "test trap correlation matrix")
#Now get p values
install.packages("Hmisc")
library(Hmisc)
cortraps.ps<-rcorr(as.matrix(hes4[,c(1:5,7,9,10,13,16:22)]), type="spearman")$P
write.csv(cortraps.ps, "trap correlation matrix")

#now PCA on habitat variables at site level
hes5 <- read.table("site level PCA input data.csv", header=TRUE, row.names="Site", sep=",")
pcasite<-prcomp(hes5, scale=TRUE)
summary(pcasite)
pcasite$sdev^2
biplot(pcasite)
write.csv(pcasite$x, 'pca site scores')
write.csv(pcasite$rotation, 'site pca var scores')

#now multi-model inference of PCA axes against community variables
hes6 <- read.table("site level PCA modelling.csv", header=TRUE, row.names="Site", sep=",")
names(hes6)
install.packages("MuMIn")
library(MuMIn)
options(na.action = "na.fail")
install.packages("AICcmodavg")
library(AICcmodavg)
#first abundance 
plot(hes6$PCA1, hes6$abundance)
ab.mod1<-lm(abundance~PCA1+PCA2+PCA3+PCA4, data=hes6)
summary(ab.mod1)
mms1<-dredge(ab.mod1)
#look at model with PC3
#try quadratic and cubic models
PC12<-hes6$PCA1^2
PC13<-hes6$PCA1^3
#now try comparing linear, quadratic and cubic models
ab.mod2<-lm(hes6$abundance~hes6$PCA1)
summary(ab.mod2)
plot(hes6$PCA1,hes6$abundance)
lines(hes6$PCA1, predict(ab.mod2))
AICc(ab.mod2, second.ord=TRUE)
ab.mod3<-lm(hes6$abundance~hes6$PCA1+PC12)
summary(ab.mod3)
plot(hes6$PCA1,hes6$abundance)
lines(hes6$PCA1, predict(ab.mod3))
AICc(ab.mod3, second.ord=TRUE)
ab.mod4<-lm(hes6$abundance~hes6$PCA1+PC12+PC13)
summary(ab.mod4)
plot(hes6$PCA1,hes6$abundance)
lines(hes6$PCA1, predict(ab.mod4))
AICc(ab.mod4, second.ord=TRUE)
#so quadratic best. check residuals etc
par(mfrow=c(2,2))
plot(ab.mod3)
#looks fine - go with quadratic

#now richness
par(mfrow=c(1,1))
plot(hes6$PCA1, hes6$richness)
#better log10 richness
logrich<-log10(hes6$richness)
plot(hes6$PCA1, logrich)
ri.mod1<-lm(logrich~hes6$PCA1+hes6$PCA2+hes6$PCA3+hes6$PCA4)
summary(ri.mod1)
mms1<-dredge(ri.mod1)
mms1
#look at PC1 model
ri.mod2<-lm(logrich~hes6$PCA1)
summary(ri.mod2)
plot(hes6$PCA1,logrich)
lines(hes6$PCA1, predict(ri.mod2))
AICc(ri.mod2, second.ord=TRUE)
#now compare linear and quadratic and cubic models
ri.mod3<-lm(logrich~hes6$PCA1+PC12)
summary(ri.mod3)
plot(hes6$PCA1,logrich)
lines(hes6$PCA1, predict(ri.mod3))
AICc(ri.mod3, second.ord=TRUE)
#so quadratic better, now cubic
ri.mod4<-lm(logrich~hes6$PCA1+PC12+PC13)
summary(ri.mod4)
plot(hes6$PCA1,logrich)
lines(hes6$PCA1, predict(ri.mod4))
AICc(ri.mod4, second.ord=TRUE)
#quadratic by far best: check residuals etc
par(mfrow=c(2,2))
plot(ri.mod4)
#looks good

#now log10invsim
par(mfrow=c(1,1))
plot(hes6$PCA1, hes6$invsim)
#better log10 invsim
loginvsim<-log10(hes6$invsim)
plot(hes6$PCA1, loginvsim)
is.mod1<-lm(loginvsim~hes6$PCA1+hes6$PCA2+hes6$PCA3+hes6$PCA4)
summary(is.mod1)
mms1<-dredge(is.mod1)
mms1
#look at PC1 model
is.mod2<-lm(loginvsim~hes6$PCA1)
summary(is.mod2)
plot(hes6$PCA1,loginvsim)
lines(hes6$PCA1, predict(is.mod2))
AICc(is.mod2, second.ord=TRUE)
#now compare linear and quadratic and cubic models
PC12<-(hes6$PCA1)^2
is.mod3<-lm(loginvsim~hes6$PCA1+PC12)
summary(is.mod3)
plot(hes6$PCA1,loginvsim)
lines(hes6$PCA1, predict(is.mod3))
AICc(is.mod3, second.ord=TRUE)
#so linear better: check residuals
par(mfrow=c(2,2))
plot(is.mod2)
#looks good
#look at model of both PC1 and PC2
is.mod4<-lm(loginvsim~hes6$PCA1+hes6$PCA2)
summary(is.mod4)
#PC2 not sig.

#now shannon
par(mfrow=c(1,1))
plot(hes6$PCA1, hes6$shannon)
sh.mod1<-lm(hes6$shannon~hes6$PCA1+hes6$PCA2+hes6$PCA3+hes6$PCA4)
summary(sh.mod1)
mms1<-dredge(sh.mod1)
mms1
#look at PC1 model
sh.mod2<-lm(hes6$shannon~hes6$PCA1)
summary(sh.mod2)
plot(hes6$PCA1,hes6$shannon)
lines(hes6$PCA1, predict(sh.mod2))
AICc(sh.mod2, second.ord=TRUE)
#now compare linear and quadratic models
PC12<-(hes6$PCA1)^2
sh.mod3<-lm(hes6$shannon~hes6$PCA1+PC12)
summary(sh.mod3)
plot(hes6$PCA1,hes6$shannon)
lines(hes6$PCA1, predict(sh.mod3))
AICc(sh.mod3, second.ord=TRUE)
#so linear better: check residuals
par(mfrow=c(2,2))
plot(sh.mod2)
#looks good
#look at model with PC1 and PC2
sh.mod4<-lm(hes6$shannon~hes6$PCA1+hes6$PCA2)
summary(sh.mod4)
#PC2 not sig

#now NMDS at site level
hes7<- read.table("Site level data.csv", header=TRUE, row.names="Site", sep=",", check.names = FALSE)
library(vegan)
sitenmds<-metaMDS(hes7)
sitenmds

#tells us we have used the bray-curtis dissimilarity matrix (the default) to run the ordination
#and we get two NMDS variables (dimensions) output (the default too)


par(mfrow=c(1,1))
plot(sitenmds)
#this plots sites (circles) and species (+) against the two computed axes. 
#sites with similar axes scores (close together) have similar species compositions
#species with similar scores are found at similar sites (more likely to be found together)
#to see what site is what and what species is what we can plot the graph in more detail

plot(sitenmds, type = "n")
#start with a bare plot, no data

text(sitenmds, display = "sites", cex = 0.8, pch=21, col="red", bg="yellow") 
#plot the sites as text
#you can see that sites at the bottom of the mountain are on the left


text(sitenmds, display = "spec", cex=0.7, col="blue")

scores(sitenmds)
write.csv(scores(sitenmds, display = "sites"), "siteNMDSsitescores.csv")
write.csv(scores(sitenmds, display="species"), "siteNMDSspeciescores.csv")

#need to create nice figure of the biplot
#first make a dataframe of the site scores
nmds.scores <- as.data.frame(scores(sitenmds, display="sites"))  #Using the scores function from vegan to extract the site scores and convert to a data.frame
nmds.scores$site <- rownames(nmds.scores)  # create a column of site names, from the rownames of data.scores
head(nmds.scores)
#now make dataframe of the species scores
species.scores <- as.data.frame(scores(sitenmds, "species"))  #Using the scores function from vegan to extract the species scores and convert to a data.frame
species.scores$species <- rownames(species.scores)  # create a column of species, from the rownames of species.scores
head(species.scores) 
#now plot the biplot
library(ggplot2)
Fig6<-ggplot() + 
  geom_text(data=species.scores,aes(x=NMDS1,y=NMDS2,label=species),size=3, fontface = "italic", alpha=0.5) +  # add the species labels
  geom_point(data=nmds.scores,aes(x=NMDS1,y=NMDS2),size=2) + # add the point markers
  geom_text(data=nmds.scores,aes(x=NMDS1,y=NMDS2,label=site),size=5,vjust=0,hjust=0) +  # add the site labels
  scale_y_continuous(breaks = seq(-1.5, 1.5, by = 0.5))+
  scale_x_continuous(breaks = seq(-2, 2, by = 0.5), limits=c(-2,2))+
  coord_equal() +
  theme_bw() + 
  theme(
        panel.background = element_blank(), 
        panel.grid.major = element_blank(),  #remove major-grid labels
        panel.grid.minor = element_blank(),  #remove minor-grid labels
        plot.background = element_blank())

ggsave("Figure6.tiff", units="cm", width=20, height=15, dpi=300, compression = 'lzw')

install.packages("MuMIn")
library(MuMIn)
#now mms of NMDS1 and NMDS2
par(mfrow=c(1,1))
plot(hes6$PCA1, hes6$NMDS1)
nmds.mod1<-lm(NMDS1~PCA1+PCA2+PCA3+PCA4, data=hes6)
summary(nmds.mod1)
mms1<-dredge(nmds.mod1)
mms1
#now get the best model
nmds.mod2<-lm(hes6$NMDS1~hes6$PCA1+hes6$PCA2)
summary(nmds.mod2)
#check residuals etc
par(mfrow=c(2,2))
plot(nmds.mod2)
#looks fine now NMDS2
par(mfrow=c(1,1))
plot(hes6$PCA1,hes6$NMDS2)
nmds.mod3<-lm(NMDS2~PCA1+PCA2+PCA3+PCA4, data=hes6)
summary(nmds.mod3)
mms1<-dredge(nmds.mod3)
mms1
#try PCA2 model
nmds.mod4<-lm(NMDS2~PCA2, data=hes6)
summary(nmds.mod4)
#not sig

#now figure of PCA1 v NMDS1
cleanup=theme(panel.grid.major = element_blank(),
              panel.grid.minor = element_blank(),
              panel.background = element_blank(),
              axis.line = element_line(color = "black"),
              axis.line.x.top = element_line(color = "black"),
              axis.line.y.right = element_line(color = "black"))


Fig7=ggplot(hes6, aes(PCA1, NMDS1))+
  geom_point()+
  xlab("PC1")+
  ylab("NMDS1")+
  coord_cartesian(ylim=c(-1.5,2.0)) +
  geom_smooth(method=lm, color="black", fullrange=FALSE)+
  scale_y_continuous(breaks = seq(-1.5, 2.0, by = 0.5))+
  scale_x_continuous(breaks = seq(-6, 3.5, by = 1), limits=c(-6,3.5))+
  cleanup



ggsave("Figure7.tiff", units="cm", width=20, height=15, dpi=300, compression = 'lzw')


#now PCA on habitat variables at trap level
hes8 <- read.table("trap level PCA input data.csv", header=TRUE, row.names="Trap", sep=",")
pcatrap<-prcomp(hes8, scale=TRUE)
summary(pcatrap)
pcatrap$sdev^2
biplot(pcatrap)
write.csv(pcatrap$x, 'pca trap scores')
write.csv(pcatrap$rotation, 'trap pca var scores')

#now multi-model inference of PCA axes against community variables
hes9 <- read.table("trap level PCA model data.csv", header=TRUE, row.names="trap", sep=",")
names(hes9)
install.packages("MuMIn")
library(MuMIn)
options(na.action = "na.fail")
install.packages("AICcmodavg")
library(AICcmodavg)

#first abundance 
plot(hes9$PCA1, hes9$abundance)
ab.mod1<-lm(abundance~PCA1+PCA2+PCA3+PCA4, data=hes9)
summary(ab.mod1)
mms1<-dredge(ab.mod1)
mms1
#now try comparing linear, quadratic and cubic models of PC1
ab.mod2<-lm(hes9$abundance~hes9$PCA1)
summary(ab.mod2)
plot(hes9$PCA1,hes9$abundance)
lines(hes9$PCA1, predict(ab.mod2))
AICc(ab.mod2, second.ord=TRUE)
PCA12<-(hes9$PCA1)^2
PCA13<-(hes9$PCA1)^3
ab.mod3<-lm(hes9$abundance~hes9$PCA1+PCA12)
summary(ab.mod3)
plot(hes9$PCA1,hes9$abundance)
lines(hes9$PCA1, predict(ab.mod3))
AICc(ab.mod3, second.ord=TRUE)
ab.mod4<-lm(hes9$abundance~hes9$PCA1+PCA12+PCA13)
summary(ab.mod4)
plot(hes9$PCA1,hes9$abundance)
lines(hes9$PCA1, predict(ab.mod4))
AICc(ab.mod4, second.ord=TRUE)
#so quadratic best. check residuals etc
par(mfrow=c(2,2))
plot(ab.mod3)
#looks fine - go with quadratic

#now richness
par(mfrow=c(1,1))
plot(hes9$PCA1, hes9$richness)
#better log10 richness
logrich<-log10(hes9$richness)
plot(hes9$PCA1, logrich)
ri.mod1<-lm(logrich~hes9$PCA1+hes9$PCA2+hes9$PCA3+hes9$PCA4)
summary(ri.mod1)
mms1<-dredge(ri.mod1)
mms1
#look at PC1 model
ri.mod2<-lm(logrich~hes9$PCA1)
summary(ri.mod2)
plot(hes9$PCA1,logrich)
lines(hes9$PCA1, predict(ri.mod2))
AICc(ri.mod2, second.ord=TRUE)
#now compare linear and quadratic and cubic models
ri.mod3<-lm(logrich~hes9$PCA1+PCA12)
summary(ri.mod3)
plot(hes9$PCA1,logrich)
lines(hes9$PCA1, predict(ri.mod3))
AICc(ri.mod3, second.ord=TRUE)
#so quadratic better, now cubic
ri.mod4<-lm(logrich~hes9$PCA1+PCA12+PCA13)
summary(ri.mod4)
plot(hes9$PCA1,logrich)
lines(hes9$PCA1, predict(ri.mod4))
AICc(ri.mod4, second.ord=TRUE)
#quadratic best: check residuals etc
par(mfrow=c(2,2))
plot(ri.mod3)
#looks good

#now log10invsim
par(mfrow=c(1,1))
plot(hes9$PCA1, hes9$invsim)
#better log10 invsim
loginvsim<-log10(hes9$invsim)
plot(hes9$PCA1, loginvsim)
is.mod1<-lm(loginvsim~hes9$PCA1+hes9$PCA2+hes9$PCA3+hes9$PCA4)
summary(is.mod1)
mms1<-dredge(is.mod1)
mms1
#look at PC1 model
is.mod2<-lm(loginvsim~hes9$PCA1)
summary(is.mod2)
plot(hes9$PCA1,loginvsim)
lines(hes9$PCA1, predict(is.mod2))
AICc(is.mod2, second.ord=TRUE)
#now compare linear and quadratic and cubic models
is.mod3<-lm(loginvsim~hes9$PCA1+PCA12)
summary(is.mod3)
plot(hes9$PCA1,loginvsim)
lines(hes9$PCA1, predict(is.mod3))
AICc(is.mod3, second.ord=TRUE)
#so linear better: check residuals
par(mfrow=c(2,2))
plot(is.mod2)
#looks good
#look at model of both PC1 and PC2
is.mod4<-lm(loginvsim~hes9$PCA1+hes9$PCA2)
summary(is.mod4)
#PC2 not sig.

#now shannon
par(mfrow=c(1,1))
plot(hes9$PCA1, hes9$shannon)
sh.mod1<-lm(hes9$shannon~hes9$PCA1+hes9$PCA2+hes9$PCA3+hes9$PCA4)
summary(sh.mod1)
mms1<-dredge(sh.mod1)
mms1
#look at PC1 model
sh.mod2<-lm(hes9$shannon~hes9$PCA1)
summary(sh.mod2)
plot(hes9$PCA1,hes9$shannon)
lines(hes9$PCA1, predict(sh.mod2))
AICc(sh.mod2, second.ord=TRUE)
#now compare linear and quadratic models
sh.mod3<-lm(hes9$shannon~hes9$PCA1+PCA12)
summary(sh.mod3)
plot(hes9$PCA1,hes9$shannon)
lines(hes9$PCA1, predict(sh.mod3))
AICc(sh.mod3, second.ord=TRUE)
#so linear better: check residuals
par(mfrow=c(2,2))
plot(sh.mod2)
#looks good
#look at model with PC1 and PC2
sh.mod4<-lm(hes9$shannon~hes9$PCA1+hes9$PCA2)
summary(sh.mod4)
#PC2 not sig

#now NMDS at trap level
hes10<- read.table("Trap level data.csv", header=TRUE, row.names="Site", sep=",", check.names = FALSE)
library(vegan)
trapnmds<-metaMDS(hes10)
trapnmds

#tells us we have used the bray-curtis dissimilarity matrix (the default) to run the ordination
#and we get two NMDS variables (dimensions) output (the default too)


par(mfrow=c(1,1))
plot(trapnmds)
#this plots sites (circles) and species (+) against the two computed axes. 
#sites with similar axes scores (close together) have similar species compositions
#species with similar scores are found at similar sites (more likely to be found together)
#to see what site is what and what species is what we can plot the graph in more detail

plot(trapnmds, type = "n")
#start with a bare plot, no data

text(trapnmds, display = "sites", cex = 0.8, pch=21, col="red", bg="yellow") 
#plot the sites as text
#you can see that sites at the bottom of the mountain are on the left


text(trapnmds, display = "spec", cex=0.7, col="blue")

scores(sitenmds)
write.csv(scores(sitenmds, display = "sites"), "trapNMDSsitescores.csv")
write.csv(scores(sitenmds, display="species"), "trapNMDSspeciescores.csv")

#need to create nice figure of the biplot
#first make a dataframe of the site scores
nmds.scores <- as.data.frame(scores(trapnmds, display="sites"))  #Using the scores function from vegan to extract the site scores and convert to a data.frame
nmds.scores$site <- rownames(nmds.scores)  # create a column of site names, from the rownames of data.scores
head(nmds.scores)
#now make dataframe of the species scores
species.scores <- as.data.frame(scores(trapnmds, "species"))  #Using the scores function from vegan to extract the species scores and convert to a data.frame
species.scores$species <- rownames(species.scores)  # create a column of species, from the rownames of species.scores
head(species.scores) 
#now plot the biplot
library(ggplot2)
FigS1<-ggplot() + 
  geom_text(data=species.scores,aes(x=NMDS1,y=NMDS2,label=species),size=3, fontface = "italic", alpha=0.5) +  # add the species labels
  geom_point(data=nmds.scores,aes(x=NMDS1,y=NMDS2),size=2) + # add the point markers
  geom_text(data=nmds.scores,aes(x=NMDS1,y=NMDS2,label=site),size=5,vjust=0,hjust=0) +  # add the site labels
  scale_y_continuous(breaks = seq(-1.5, 1.5, by = 0.5))+
  scale_x_continuous(breaks = seq(-2, 2, by = 0.5), limits=c(-2,2))+
  coord_equal() +
  theme_bw() + 
  theme(
    panel.background = element_blank(), 
    panel.grid.major = element_blank(),  #remove major-grid labels
    panel.grid.minor = element_blank(),  #remove minor-grid labels
    plot.background = element_blank())

ggsave("FigS1.tiff", units="cm", width=20, height=15, dpi=300, compression = 'lzw')

#now models of NMDS against PCs at trap level
par(mfrow=c(1,1))
plot(hes9$PCA1, hes9$NMDS1)
nmds.mod1<-lm(NMDS1~PCA1+PCA2+PCA3+PCA4, data=hes9)
summary(nmds.mod1)
mms1<-dredge(nmds.mod1)
mms1
#now get the best model
nmds.mod2<-lm(hes9$NMDS1~hes9$PCA1+hes9$PCA3+hes9$PCA4)
summary(nmds.mod2)
#check residuals etc
par(mfrow=c(2,2))
plot(nmds.mod2)
#looks fine now NMDS2
par(mfrow=c(1,1))
plot(hes9$PCA1,hes9$NMDS2)
nmds.mod3<-lm(NMDS2~PCA1+PCA2+PCA3+PCA4, data=hes9)
summary(nmds.mod3)
mms1<-dredge(nmds.mod3)
mms1
#try PCA2 model
nmds.mod4<-lm(NMDS2~PCA2, data=hes9)
summary(nmds.mod4)
#not sig

#now figure of PCA1 v NMDS1
cleanup=theme(panel.grid.major = element_blank(),
              panel.grid.minor = element_blank(),
              panel.background = element_blank(),
              axis.line = element_line(color = "black"),
              axis.line.x.top = element_line(color = "black"),
              axis.line.y.right = element_line(color = "black"))


FigS2=ggplot(hes9, aes(PCA1, NMDS1))+
  geom_point()+
  xlab("PC1")+
  ylab("NMDS1")+
  coord_cartesian(ylim=c(-1.5,1.5)) +
  geom_smooth(method=lm, color="black", fullrange=FALSE)+
  scale_y_continuous(breaks = seq(-1.5, 1.5, by = 0.5))+
  scale_x_continuous(breaks = seq(-2.5, 2.5, by = 0.5), limits=c(-2.5,2.5))+
  cleanup



ggsave("FigureS2.tiff", units="cm", width=20, height=15, dpi=300, compression = 'lzw')

#now models of sites against vegetation variables including elevation. 
alt1<-read.table("site level summary.csv", header=TRUE, sep=",")
names(alt1)
e2<-alt1$elevation^2
e3<-alt1$elevation^3
e4<-alt1$elevation^4
mod1<-lm(alt1$Total.site.abundance~alt1$elevation +e2 +e3 
         +alt1$N.plants.DBH...10cm
         +alt1$N.plants.DBH..10cm + alt1$N.Palm.trees + alt1$N.Fern.trees
         +alt1$Bamboo.ground.cover+ alt1$Fern.ground.cover
         +alt1$herb.ground.cover + alt1$Total.epiphytes
         +alt1$Total.lianas)
summary(mod1)
mms10<-dredge(mod1, fixed = c("alt1$elevation", "e2", "e3"))
mms10
#best model only with elevation terms, none other close
#now log richness
logrich<-log10(alt1$Total.site.richness)
mod2<-lm(logrich~alt1$elevation +e2 
         +alt1$N.plants.DBH...10cm
         +alt1$N.plants.DBH..10cm + alt1$N.Palm.trees + alt1$N.Fern.trees
         +alt1$Bamboo.ground.cover+ alt1$Fern.ground.cover
         +alt1$herb.ground.cover + alt1$Total.epiphytes
         +alt1$Total.lianas)
summary(mod2)
mms11<-dredge(mod2, fixed = c("alt1$elevation", "e2"))
mms11
#again best model just with elevation terms
#look at other models
mod21<-lm(logrich~alt1$elevation +e2 
         + alt1$Fern.ground.cover)
summary(mod21)
mod22<-lm(logrich~alt1$elevation +e2 
         
         +alt1$herb.ground.cover )
summary(mod22)
#now log invsim
loginv<-log10(alt1$invsim)
mod3<-lm(loginv~alt1$elevation 
         +alt1$N.plants.DBH...10cm
         +alt1$N.plants.DBH..10cm + alt1$N.Palm.trees + alt1$N.Fern.trees
         +alt1$Bamboo.ground.cover+ alt1$Fern.ground.cover
         +alt1$herb.ground.cover + alt1$Total.epiphytes
         +alt1$Total.lianas)
summary(mod3)
mms12<-dredge(mod3, fixed = c("alt1$elevation"))
mms12
#best model with elevation and herbs, none others were close
#look at this model
mod3b<-lm(loginv~alt1$elevation 
         +alt1$herb.ground.cover)
summary(mod3b)
#now Shannon

mod4<-lm(alt1$shannon~alt1$elevation +e2
         +alt1$N.plants.DBH...10cm
         +alt1$N.plants.DBH..10cm + alt1$N.Palm.trees + alt1$N.Fern.trees
         +alt1$Bamboo.ground.cover+ alt1$Fern.ground.cover
         +alt1$herb.ground.cover + alt1$Total.epiphytes
         +alt1$Total.lianas)
summary(mod4)
mms13<-dredge(mod4, fixed = c("alt1$elevation","e2"))
mms13
#best model again with elevation and herb ground cover
#look at this model
mod41<-lm(alt1$shannon~alt1$elevation +e2
         
         +alt1$herb.ground.cover )
summary(mod41)
#now NMDS1
mod5<-lm(alt1$NMDS1~alt1$elevation
         +alt1$N.plants.DBH...10cm
         +alt1$N.plants.DBH..10cm + alt1$N.Palm.trees + alt1$N.Fern.trees
         +alt1$Bamboo.ground.cover+ alt1$Fern.ground.cover
         +alt1$herb.ground.cover + alt1$Total.epiphytes
         +alt1$Total.lianas)
summary(mod5)
mms14<-dredge(mod5, fixed = c("alt1$elevation"))
mms14
plot(alt1$elevation, alt1$NMDS1)
#looks linear
#look at best model
mod51<-lm(alt1$NMDS1~alt1$elevation
         + alt1$Total.epiphytes
         )
summary(mod51)

#now models without elevation
#start with abundance.
mod6<-lm(alt1$Total.site.abundance~
         +alt1$N.plants.DBH...10cm
         +alt1$N.plants.DBH..10cm + alt1$N.Palm.trees + alt1$N.Fern.trees
         +alt1$Bamboo.ground.cover+ alt1$Fern.ground.cover
         +alt1$herb.ground.cover + alt1$Total.epiphytes
         +alt1$Total.lianas)
summary(mod6)
mms15<-dredge(mod6)
summary(model.avg(mms15, subset = delta <= 2))
#now log richness
mod7<-lm(logrich~
           +alt1$N.plants.DBH...10cm
         +alt1$N.plants.DBH..10cm + alt1$N.Palm.trees + alt1$N.Fern.trees
         +alt1$Bamboo.ground.cover+ alt1$Fern.ground.cover
         +alt1$herb.ground.cover + alt1$Total.epiphytes
         +alt1$Total.lianas)
summary(mod7)
mms16<-dredge(mod7)
mms16
summary(model.avg(mms16, subset = delta <= 2))
#plot best model
plot(alt1$Total.lianas, logrich)

#now log invsim
mod8<-lm(loginv~
           +alt1$N.plants.DBH...10cm
         +alt1$N.plants.DBH..10cm + alt1$N.Palm.trees + alt1$N.Fern.trees
         +alt1$Bamboo.ground.cover+ alt1$Fern.ground.cover
         +alt1$herb.ground.cover + alt1$Total.epiphytes
         +alt1$Total.lianas)
summary(mod8)
mms17<-dredge(mod8)
mms17
summary(model.avg(mms17, subset = delta <= 2))
#plot best and only model
mod9<-lm(loginv~
           
         +alt1$Bamboo.ground.cover+ 
         +alt1$herb.ground.cover )
summary(mod9)
#plot best model
plot(alt1$Bamboo.ground.cover, loginv)
plot(alt1$herb.ground.cover, loginv)

#now shannon
mod10<-lm(alt1$shannon~
           +alt1$N.plants.DBH...10cm
         +alt1$N.plants.DBH..10cm + alt1$N.Palm.trees + alt1$N.Fern.trees
         +alt1$Bamboo.ground.cover+ alt1$Fern.ground.cover
         +alt1$herb.ground.cover + alt1$Total.epiphytes
         +alt1$Total.lianas)
summary(mod10)
mms18<-dredge(mod10)
mms18
summary(model.avg(mms18, subset = delta <= 2))
#plot best predictor
plot(alt1$Total.epiphytes, alt1$shannon)

#now NMDS1
mod11<-lm(alt1$NMDS1~
            +alt1$N.plants.DBH...10cm
          +alt1$N.plants.DBH..10cm + alt1$N.Palm.trees + alt1$N.Fern.trees
          +alt1$Bamboo.ground.cover+ alt1$Fern.ground.cover
          +alt1$herb.ground.cover + alt1$Total.epiphytes
          +alt1$Total.lianas)
summary(mod11)
mms19<-dredge(mod11)
mms19
summary(model.avg(mms19, subset = delta <= 2))
#plot best predictor
plot(alt1$Bamboo.ground.cover, alt1$NMDS1)

#now trap level models
alt2<-read.table("trap level summary data.csv", header=TRUE, sep=",")
names(alt2)
e2<-alt2$elevation^2
e3<-alt2$elevation^3
#first models constrained to include elevation
#for abundance includes e2 and e3
mod12<-lm(alt2$Total.abundance~alt2$elevation +e2 +e3 
         +alt2$N.plants.DBH...10cm
         +alt2$N.plants.DBH..10cm + alt2$N.Palm.trees + alt2$N.Fern.trees
         +alt2$Bamboo.ground.cover+ alt2$Fern.ground.cover
         +alt2$herb.ground.cover + alt2$total.epiphytes
         +alt2$total.lianas)
summary(mod12)
mms20<-dredge(mod12, fixed = c("alt2$elevation", "e2", "e3"))
mms20
summary(model.avg(mms20, subset = delta <= 2))
#now log richness, contrained with e and e2
logrich<-log10(alt2$Total.trap.richness)
mod13<-lm(logrich~alt2$elevation +e2 
          +alt2$N.plants.DBH...10cm
          +alt2$N.plants.DBH..10cm + alt2$N.Palm.trees + alt2$N.Fern.trees
          +alt2$Bamboo.ground.cover+ alt2$Fern.ground.cover
          +alt2$herb.ground.cover + alt2$total.epiphytes
          +alt2$total.lianas)
summary(mod13)
mms21<-dredge(mod13, fixed = c("alt2$elevation", "e2"))
mms21
summary(model.avg(mms21, subset = delta <= 2))
#plot best predictor
plot(alt2$total.epiphytes, logrich)

#now log invsim with e and e2
loginv<-log10(alt2$Invsim)
mod14<-lm(loginv~alt2$elevation +e2 
          +alt2$N.plants.DBH...10cm
          +alt2$N.plants.DBH..10cm + alt2$N.Palm.trees + alt2$N.Fern.trees
          +alt2$Bamboo.ground.cover+ alt2$Fern.ground.cover
          +alt2$herb.ground.cover + alt2$total.epiphytes
          +alt2$total.lianas)
summary(mod14)
mms22<-dredge(mod14, fixed = c("alt2$elevation", "e2"))
mms22
summary(model.avg(mms22, subset = delta <= 2))
#plot best predictors
plot(alt2$total.epiphytes, loginv)
plot(+alt2$herb.ground.cover, loginv)

#now shannon, with e and e2
mod15<-lm(alt2$shannon~alt2$elevation +e2 
          +alt2$N.plants.DBH...10cm
          +alt2$N.plants.DBH..10cm + alt2$N.Palm.trees + alt2$N.Fern.trees
          +alt2$Bamboo.ground.cover+ alt2$Fern.ground.cover
          +alt2$herb.ground.cover + alt2$total.epiphytes
          +alt2$total.lianas)
summary(mod15)
mms23<-dredge(mod15, fixed = c("alt2$elevation", "e2"))
mms23
summary(model.avg(mms23, subset = delta <= 2))
#plot best predictors
plot(alt2$total.epiphytes, alt2$shannon)
plot(+alt2$herb.ground.cover, alt2$shannon)

# now NMDS1 with elevation
mod16<-lm(alt2$NMDS1~alt2$elevation 
          +alt2$N.plants.DBH...10cm
          +alt2$N.plants.DBH..10cm + alt2$N.Palm.trees + alt2$N.Fern.trees
          +alt2$Bamboo.ground.cover+ alt2$Fern.ground.cover
          +alt2$herb.ground.cover + alt2$total.epiphytes
          +alt2$total.lianas)
summary(mod16)
mms24<-dredge(mod16, fixed = c("alt2$elevation"))
mms24
summary(model.avg(mms24, subset = delta <= 2))
#plot best predictor
plot(alt2$total.epiphytes, alt2$NMDS1)

#now models of trap level without elevation
#start with abundance. 
mod17<-lm(alt2$Total.abundance~
          +alt2$N.plants.DBH...10cm
          +alt2$N.plants.DBH..10cm + alt2$N.Palm.trees + alt2$N.Fern.trees
          +alt2$Bamboo.ground.cover+ alt2$Fern.ground.cover
          +alt2$herb.ground.cover + alt2$total.epiphytes
          +alt2$total.lianas)
summary(mod17)
mms25<-dredge(mod17)
mms25
summary(model.avg(mms25, subset = delta <= 2))
#now log richness
logrich<-log10(alt2$Total.trap.richness)
mod18<-lm(logrich~
          +alt2$N.plants.DBH...10cm
          +alt2$N.plants.DBH..10cm + alt2$N.Palm.trees + alt2$N.Fern.trees
          +alt2$Bamboo.ground.cover+ alt2$Fern.ground.cover
          +alt2$herb.ground.cover + alt2$total.epiphytes
          +alt2$total.lianas)
summary(mod18)
mms26<-dredge(mod18)
mms26
summary(model.avg(mms26, subset = delta <= 2))
#plot best predictor
plot(alt2$total.epiphytes, logrich)
#now log invsimp
loginv<-log10(alt2$Invsim)
mod19<-lm(loginv~ 
          +alt2$N.plants.DBH...10cm
          +alt2$N.plants.DBH..10cm + alt2$N.Palm.trees + alt2$N.Fern.trees
          +alt2$Bamboo.ground.cover+ alt2$Fern.ground.cover
          +alt2$herb.ground.cover + alt2$total.epiphytes
          +alt2$total.lianas)
summary(mod19)
mms27<-dredge(mod19)
mms27
summary(model.avg(mms27, subset = delta <= 2))
#look at best and only model
model28<-lm(loginv~alt2$N.plants.DBH..10cm +alt2$herb.ground.cover 
            + alt2$total.epiphytes)
summary(model28)
par(mfrow=c(2,2))
plot(model28)
#looks great
#now shannon
mod20<-lm(alt2$shannon~
          +alt2$N.plants.DBH...10cm
          +alt2$N.plants.DBH..10cm + alt2$N.Palm.trees + alt2$N.Fern.trees
          +alt2$Bamboo.ground.cover+ alt2$Fern.ground.cover
          +alt2$herb.ground.cover + alt2$total.epiphytes
          +alt2$total.lianas)
summary(mod20)
mms29<-dredge(mod20)
mms29
summary(model.avg(mms29, subset = delta <= 2))
#now NMDS1
mod21<-lm(alt2$NMDS1~
          +alt2$N.plants.DBH...10cm
          +alt2$N.plants.DBH..10cm + alt2$N.Palm.trees + alt2$N.Fern.trees
          +alt2$Bamboo.ground.cover+ alt2$Fern.ground.cover
          +alt2$herb.ground.cover + alt2$total.epiphytes
          +alt2$total.lianas)
summary(mod21)
mms30<-dredge(mod21, fixed = c("alt2$elevation"))
mms30
summary(model.avg(mms30, subset = delta <= 2))

#now a figure of vegetation variables against trap level community properties

#Plot Figure 8
library(ggplot2)

#set theme
cleanup=theme(panel.grid.major = element_blank(),
              panel.grid.minor = element_blank(),
              panel.background = element_blank(),
              axis.line = element_line(color = "black"),
              axis.line.x.top = element_line(color = "black"),
              axis.line.y.right = element_line(color = "black"))

#Plot Fig8a

#create table of log richness and epiphytes
fig8atab<-data.frame(alt2$total.epiphytes, logrich)
plot(alt2$total.epiphytes, logrich)
Fig8a = ggplot(fig8atab, aes(alt2$total.epiphytes, logrich))+
  geom_point()+
  xlab("Epiphyte density")+
  ylab(bquote(Log[10]~'Richness'))+
  scale_y_continuous(breaks = seq(0, 1.5, by = 0.5))+
  scale_x_continuous(breaks = seq(0, 20, by = 5), limits=c(0,20))+
  cleanup       

#now invsim against herbs
Fig8btab<-data.frame(alt2$herb.ground.cover, loginv)
plot(alt2$herb.ground.cover, loginv)
Fig8b = ggplot(Fig8btab, aes(alt2$herb.ground.cover, loginv))+
  geom_point()+
  xlab("Herb ground cover")+
  ylab(bquote(Log[10] (1/D)))+
  scale_y_continuous(breaks = seq(0, 1.4, by = 0.2), limits = c(0, 1.4))+
  scale_x_continuous(breaks = seq(0, 3, by = 0.5), limits=c(0,3))+
  cleanup  

plot(alt2$total.epiphytes, alt2$shannon)
Fig8c = ggplot(alt2, aes(alt2$total.epiphytes, alt2$shannon))+
  geom_point()+
  xlab("Epiphyte density")+
  ylab("Shannon diversity")+
  scale_y_continuous(breaks = seq(0, 3.5, by = 0.5), limits = c(0, 3.5))+
  scale_x_continuous(breaks = seq(0, 20, by = 5), limits=c(0,20))+
  cleanup  

#now bamboo against NMDS1
plot(alt2$Bamboo.ground.cover, alt2$NMDS1)
Fig8d = ggplot(alt2, aes(alt2$Bamboo.ground.cover, alt2$NMDS1))+
  geom_point()+
  xlab("Bamboo ground cover")+
  ylab("NMDS1")+
  scale_y_continuous(breaks = seq(-1.5, 2, by = 0.5), limits=c(-1.5, 2))+
  scale_x_continuous(breaks = seq(0, 3, by = 0.5), limits=c(0,3))+
  cleanup  
#now bamboo against NMDS

install.packages("ggpubr")
library("ggpubr")
Figure8<-ggarrange(Fig8a, Fig8b, Fig8c, Fig8d,
                   labels = c("(a)", "(b)", "(c)", "(d)"),
                   hjust=0,
                   ncol = 2, nrow = 2)

ggsave("Figure8.tiff", units="cm", width=20, height=15, dpi=300, compression = 'lzw')
